package com.fuce.xiaoai.Controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.fuce.xiaoai.Util.AiRturnUtil;
import com.fuce.xiaoai.model.accept.AcceptJson;
import com.fuce.xiaoai.model.returns.ReturnJson;
import com.google.gson.Gson;

@Controller
@RequestMapping("api")
public class Apicontroller {
	
	
	@GetMapping("get")
	@ResponseBody
	public ReturnJson get(HttpServletRequest request) {
		
		System.out.println("222="+request);
		
		
		return AiRturnUtil.setText("凤凤我爱你",true,false);
		
		
	}
	
	
	
	
	
	
	@PostMapping("get")
	@ResponseBody
	public Object get2(HttpServletRequest request) throws Exception{
		
		System.out.println("222="+request);
		Gson gson=new Gson();
		String returnStr="";
		//普通数据是获取不到的 
		//接受消息
        InputStream in = request.getInputStream();  
        int len = 0;  
        byte[] buffer = new byte[1024];  
        while ((len = in.read(buffer)) > 0) {  
        	returnStr+=new String(buffer, 0, len);
            System.out.println("返回信息<<"+returnStr);  
        }  
        in.close();
        
       
        try {
        	 AcceptJson acceptJson= gson.fromJson(returnStr, AcceptJson.class);
             
             //创建返回信息
           
             returnStr="";
             ReturnJson returnJson=null;
            if(acceptJson.getRequest().getSlot_info()!=null&&"Mi_Welcome".equals(acceptJson.getRequest().getSlot_info().getIntent_name())) {
         	   returnStr="欢迎进入密码情话游戏现在开始！请对小爱说出操作指令！"; 
         	   returnJson=AiRturnUtil.setText(returnStr,false,true);
            }else {
         	   switch (acceptJson.getQuery()) {
     				case "退出":
     					returnStr="马上退出";
     					returnJson=AiRturnUtil.setText(returnStr,true,false);
     					System.out.println(gson.toJson(returnJson));
     					break;
     				case "我爱你":
     					returnStr="凤凤我爱你，爱死你了！_小册子留言";
     					 returnJson=AiRturnUtil.setText(returnStr,false,true);
     					
     					break;
     				default:
     					returnStr="我没听懂,请重新说一次";
     					returnJson=AiRturnUtil.setText(returnStr,false,true);
     					break;
     			}
         	   
         	   
            }
           
       
            //发送消息
            return gson.toJson(returnJson);
		} catch (Exception e) {
			System.out.println("错误:"+returnStr);
			
			return  gson.toJson(AiRturnUtil.setText("指令异常请重新进入",true,false));
         
		}
       
        
        
       
        
    
        
       
	}
	
	
	
}
